<?php
/**
 * 角色管理
 */
declare (strict_types = 1);

namespace app\Api\controller;

use app\validate\Roles as RolesValidate;
use think\facade\Db;
use think\Request;
use think\Response;

class Roles
{
    /**
     * 显示资源列表
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $params  = $request->get();
        $page = input($params['pageNum'],1);
        $pageSize = input($params['pageSize'],10);
        try {

            if(isset($params['keywords']) && !empty($params['keywords'])){
                $roles = Db::name('role')->where('name','like','%'.$params['keywords'].'%')->order('sort')->paginate($pageSize, false, ['page' => $page]);
            }else{
                $roles = Db::name('role')->order('sort')->paginate($pageSize, false, ['page' => $page]);
            }
            return success([
                'total'=>$roles->total(),
                'list' => $roles->items(),
                'current_page' => $roles->currentPage(),
                'last_page' => $roles->lastPage(),
            ]);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return Response
     */
    public function create()
    {
        // 查询所有可用角色列表
        $roles = Db::name('role')->where(['status'=>1])->field('id as value ,name as label')->select();
        return success($roles);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
        $validate = new RolesValidate();
        // 进行字段校验
        if(!$validate->check($data)){
            return error([],9001,$validate->getError());
        }else{
            $result=Db::name('role')->save($data);
            if($result!==false){
                return success([],0,'编辑角色成功');
            }else{
                return error([],9004,'编辑角色失败');
            }
        }
    }

    /**
     * 显示指定的资源
     *
     * @param    $id
     * @return Response
     */
    public function read($id)
    {
        //
        $data = Db::name('role')->find($id);
        if($data!=null){
            $data['menus']?$data['menus']=json_decode($data['menus']): $data['menus']=[];
            return success($data);
        }else{
            return error([],9005,'非法操作');
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @param    $id
     * @return Response
     */
    public function update( $id ,Request $request)
    {
        //先查询查看是否存在此条数据
        $data = Db::name('role')->find($id);
        $update = $request->post();
        $update['id']=$id;
        $update ['menus']=json_encode($update['menus']);
        if($data!=null){
            $result = Db::name('role')->save($update);
            if($result!==false){
                    return success([],0,'修改权限成功');
            }else{
                return error([],9004,'修改权限失败');
            }

        }else{
            return error([],9006,'此条数据不存在');
        }
    }

    /**
     * 删除指定资源
     *
     * @param    $id
     * @return Response
     */
    public function delete($id)
    {
        //
        $result = Db::name('role')->delete($id);
        if($result!==false){
            return success([],0,'删除角色成功');
        }else{
            return error([],303,'删除角色失败');
        }
    }
    public function options(){
        return 1111;
    }
}
